package com.arcgis.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.arcgis.app.MainActivity;
import com.arcgis.app.R;
import com.arcgis.app.dao.PointInfoDao;

import java.util.List;

public class ListMessageAdapter extends RecyclerView.Adapter<ListMessageAdapter.ListMessageHolder> {
    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    private OnitemClick onitemClick;
    private onLongClick onLongClick;
    private List<PointInfoDao> list;

    public ListMessageAdapter(Context context, List<PointInfoDao> data) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        list = data;
    }

    //定义设置点击事件监听的方法
    public void setOnitemClickLintener(OnitemClick onitemClick) {
        this.onitemClick = onitemClick;
    }

    public void setOnLongClickListener(onLongClick onLongClick){
        this.onLongClick = onLongClick;
    }

    public interface OnitemClick {
        void onItemClick(int position);
    }

    public  interface onLongClick{
        void onLongClick(View view, int position);
    }



    @Override
    public ListMessageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.list_cell, null, false);
        return new ListMessageHolder(view);
    }

    @Override
    public void onBindViewHolder(final ListMessageAdapter.ListMessageHolder holder, final int position) {
        PointInfoDao point = list.get(position);
        holder.locationText.setText("位置:" + point.getCurrentLocation());
        holder.phonumberText.setText("电话号码: " + point.getPhonenumber());
        holder.idCardText.setText("身份证号码: " + point.getIdCard());
        String typeStr;
        switch (point.getAdmin()){
            case 0:
                typeStr = "点";
                break;
            case 1:
                typeStr = "线";
                break;
            case 2:
                typeStr = "面";
                break;
            default:
                typeStr = "未填写";
                break;
        }
        holder.cellTypeText.setText(typeStr);
        if (onitemClick != null) {
            holder.cell_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //在TextView的地方进行监听点击事件，并且实现接口
                    onitemClick.onItemClick(position);
                }
            });
        }

        if (onLongClick != null){
            holder.cell_layout.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onLongClick.onLongClick(v,position);
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ListMessageHolder extends RecyclerView.ViewHolder {
        TextView locationText;
        TextView idCardText;
        TextView phonumberText;
        LinearLayout cell_layout;
        TextView cellTypeText;

        ListMessageHolder(View itemView) {
            super(itemView);
            locationText = itemView.findViewById(R.id.tv_message_list_cell_location);
            idCardText = itemView.findViewById(R.id.idCardText);
            phonumberText = itemView.findViewById(R.id.tv_message_list_cell_phonenumber);
            cell_layout = itemView.findViewById(R.id.ll_message_list_cell_container);
            cellTypeText = itemView.findViewById(R.id.tv_message_list_cell_type);

        }

    }
}
